package com.kongfuzi.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.kongfuzi.student.R;
import com.kongfuzi.student.support.utils.MD5Util;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.support.utils.Util;
import com.kongfuzi.student.support.volley.Response;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ResetPwdActivity extends CustomerActionBarActivity implements View.OnClickListener {

    @Bind(R.id.et_pwd_old)
    EditText mEt_old;
    @Bind(R.id.et_pwd1)
    EditText mEt_pwd1;
    @Bind(R.id.et_pwd2)
    EditText mEt_pwd2;
    @Bind(R.id.ll_old)
    LinearLayout mLl_old;
    @Bind(R.id.ll_pwd1)
    LinearLayout mLl_pwd1;
    @Bind(R.id.ll_pwd2)
    LinearLayout mLl_pwd2;

    String pwd_old;
    String pwd1;
    String pwd2;

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), ResetPwdActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
        ButterKnife.bind(this);
        setCustomerOperationText("完成");
        setCustomerOperationOnClickListener(this);
        initEvents();
    }

    private void initEvents() {
        mLl_old.setOnClickListener(this);
        mLl_pwd1.setOnClickListener(this);
        mLl_pwd2.setOnClickListener(this);
        Util.openSoftInput(getWindow().getDecorView());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_old:
                mEt_pwd1.clearFocus();
                mEt_pwd2.clearFocus();
                mEt_old.requestFocus();
                break;
            case R.id.ll_pwd1:
                mEt_pwd2.clearFocus();
                mEt_old.clearFocus();
                mEt_pwd1.requestFocus();
                break;
            case R.id.ll_pwd2:
                mEt_pwd1.clearFocus();
                mEt_old.clearFocus();
                mEt_pwd2.requestFocus();
                break;
            case R.id.tv_operation:
                if (checkData()) {
                    String requestUrl = UrlConstants.RESET_PWD + "&id=" + GasApplication
                            .getUserId() + "&pwd=" + MD5Util.encode(pwd_old) + "&newpwd=" +
                            MD5Util.encode(pwd1) + "&repwd=" + MD5Util.encode(pwd2);
                    RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            if(response!=null && response.optBoolean("success")) {
                                ToastUtil.showToast(ResetPwdActivity.this,"新密码已生效");
                                finish();
                            }
                        }
                    }, null, mTag);
                }
                break;
        }
    }

    private boolean checkData() {
        pwd_old = mEt_old.getText().toString().trim();
        pwd1 = mEt_pwd1.getText().toString().trim();
        pwd2 = mEt_pwd2.getText().toString().trim();
        if (TextUtils.isEmpty(pwd_old)) {
            ToastUtil.showToast(this, "请输入原有密码");
            return false;
        }
        if (pwd_old.length() < 6) {
            ToastUtil.showToast(this, "密码最少6位");
            return false;
        }
        if (TextUtils.isEmpty(pwd1)) {
            ToastUtil.showToast(this, "请输入新密码");
            return false;
        }
        if (pwd1.length() < 6) {
            ToastUtil.showToast(this, "密码最少6位");
            return false;
        }
        if (TextUtils.isEmpty(pwd2)) {
            ToastUtil.showToast(this, "请重复新密码");
            return false;
        }
        if (!pwd1.equals(pwd2)) {
            ToastUtil.showToast(this, "两次密码不一致");
            return false;
        }
        return true;
    }
}
